//
//  CollectionViewCell.swift
//  WeiBo
//
//  Created by 江潘 on 15/6/28.
//  Copyright © 2015年 江潘. All rights reserved.
//

import UIKit

class CollectionViewCell: UICollectionViewCell {
  
    // didset相当于属性变化监控器
    var imgIndex:Int = 0 {
    
        didSet {
         
            imgView.image = UIImage(named: "new_feature_\(imgIndex + 1)")
            
            startBtn.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupCell()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupCell(){
        addSubview(imgView)
        addSubview(startBtn)
        startBtn.addTarget(self, action: "JumpMainVc", forControlEvents: UIControlEvents.TouchUpInside)
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        self.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 80))
    }
    
    func JumpMainVc(){
    
        NSNotificationCenter.defaultCenter().postNotificationName(noticeName, object: true)
    }
    
    func showStartBtn(){
    
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        startBtn.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            
            self.startBtn.hidden = false
            
            }) { (_) -> Void in
                
                self.startBtn.userInteractionEnabled = true
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        imgView.frame = self.bounds
    }
    
    lazy var imgView:UIImageView = {
        let imgView = UIImageView()
        return imgView
    }()
    
    lazy var startBtn:UIButton = {
    
        let btn = UIButton()
        
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        btn.frame = CGRectMake(0, 0, 105, 36)
        
        return btn
    }()
}
